<?php

namespace App\Http\Controllers;

use App\GameModel;
use App\TypeModel;

use App\UserModel;
use Illuminate\Http\Request;

class GameController extends Controller
{
    public function index()
    {
        return view('admin.index');
    }

    public function type()
    {
        $data = TypeModel::all();
        return view('admin.type', compact('data'));
    }

    public function addtype()
    {
        return view('admin.addtype');
    }

    public function add(Request $request)
    {
        $input = $request->except('_token');
        TypeModel::create($input);
        return redirect('/type');
    }

    public function edit($id)
    {
        $data = TypeModel::where('id', $id)->first();
        return view('admin.edit', compact('data'));
    }

    public function editor(Request $request)
    {
        $input = $request->except('_token');
        $info = TypeModel::find($input['id']);
        $info->update($input);

        $data = TypeModel::all();
        return view('admin.type', compact('data'));
    }

    public function delete(Request $request)
    {
        try {
            $id = $request->input('id', -1);
            TypeModel::where('id', $id)->delete();
            return json_encode(['msg' => 'ok']);

        } catch (\Exception $ex) {
            return json_encode(['msg' => 'err']);
        }
    }

    public function games()
    {
        $cols = ["game.*", "type.name as type_name"];
        $data = GameModel::leftjoin('type', 'type.id', 'game.type')->orderby('game.updated_at', 'desc')->get($cols);
        return view('admin.games', compact('data'));
    }

    public function addgame()
    {
        $data = TypeModel::all();
        return view('admin.addgame', compact('data'));
    }

    public function addgamefunc(Request $request)
    {

        $file_path = 'no';
        $input = $request->except('_token', "img");
        $img = $request->file();
        foreach ($img as $k => $d) {
            $origin = $d->getClientOriginalName();
            $d->move(public_path() . "/uploads/img/", $origin);
            $file_path = "/public/uploads/img/" . $origin;
        }
        $input['img'] = $file_path;

        return redirect('/games');
    }


    public function deleteGame(Request $request)
    {
        try {
            $id = $request->input('id', -1);
            GameModel::where('id', $id)->delete();
            return json_encode(['msg' => 'ok']);

        } catch (\Exception $ex) {
            return json_encode(['msg' => 'err']);
        }
    }


    public function editgame($id)
    {
        $data = GameModel::where('id', $id)->first();
        $types = TypeModel::all();
        return view('admin.editgame', compact('data', 'types'));
    }


    public function gameEditor(Request $request)
    {
        $file_path = 'no';
        $input = $request->except('_token', "img");
        $img = $request->file();
        foreach ($img as $k => $d) {
            $origin = $d->getClientOriginalExtension();
            $dataName = date("Yid-Hdi") . mt_rand(66, 999) . "." . $origin;
            $d->move(public_path() . "/img/" . $dataName);
            $file_path = "/public/uploads/img/" . $dataName;
        }
        $input['img'] = $file_path;
        $info = GameModel::find($input['id']);
        $info->update($input);
        return redirect('/games');
    }


    public function user()
    {
        return view('admin.user');
    }


    public function desk(Request $request)
    {
        $input = $request->except('_token');
        return view('admin.desk', compact('input'));
    }

    public function createImg(Request $request)
    {
        $name = $request->input('name');
        header("content-type:image/png");
        $imgPath = realpath('public/uploads/img/12.jpg');
        $fontPath = realpath('public/uploads/img/consolaz.ttf');

        $img = imagecreatefromjpeg($imgPath);
        $color = imagecolorallocate($img, 224, 223, 15);

        imagettftext($img, 35, 0, 800, 105, $color, $fontPath, $name);
        imagejpeg($img);
        imagedestroy($img);
    }

    public function aynak()
    {
        return view("admin.aynak");
    }
}
